<?php
/* ### TEST DE CONNEXION LDAP(s) ### */  

// Configuration du compte d'authentification
$url='recette-ldap-gardian.edf.fr';
$app_user='uid=9TERU001,ou=Applis,dc=gardiansesame';
$app_pass='Mmdp-3325';
$user='AL02FECC';
$pass='Mmdp-3325';
$searchBase='ou=People,dc=gardiansesame';  // Par defaut : "c=fr"
$port=1636;

//$url='dmz-proxyldap-gardian.edf.fr';
//$app_user='uid=9TERU001,ou=Applis,dc=gardiansesame';
$app_pass='skbfZFC4ei';
//$user='AL02FECC';
$pass='sapoi4753';
//$searchBase='ou=People,dc=gardiansesame';  // Par defaut : "c=fr"
$port=636;

$ldaps=true;


function ldapauth($url, $app_user, $app_pass, $user, $pass, $searchBase, $ldaps, $port){
	$new_url=($ldaps==true) ? 'ldaps://'.$url : 'ldap://'.$url;
	echo "Before connect to $url : " . date('r') .'<br />';
	$ds=ldap_connect($new_url, $port);
	echo "Connected..." . date('r') .'<br />';
	ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
	if (is_resource($ds)){
		echo "Before Bind..." . date('r') .'<br />';
		$r=ldap_bind($ds, $app_user, $app_pass);
		if($r)
		{
			echo "Conexion avec $app_user reussi..." . date('r') .'<br />';
			echo "Before Search..." . date('r') .'<br />';
			$sr=ldap_search($ds, $searchBase, "uid=$user");
			echo "Before ldap_first_entry..." . date('r') .'<br />';
			$entry = ldap_first_entry($ds, $sr);
			echo "Before ldap_get_dn..." . date('r') .'<br />';
			$dn=ldap_get_dn($ds, $entry);
			echo "Before ldap_bind..." . date('r') .'<br />';
			$r=(ldap_bind($ds,$dn,$pass)) ? 1 : 0;
		}else{
			echo "Conexion avec $app_user refusee..." . date('r') .'<br />';	
		}
		
		echo "Before ldap_close..." . date('r') .'<br />';
		ldap_close ($ds);
	}
	return $r;
}


$resultat=ldapauth($url, $app_user, $app_pass, $user, $pass, $searchBase, $ldaps, $port);
echo "Connexion LDAP : ";
echo ($resultat==1) ?  "<b>R&eacute;ussie</b>" : "<b>Echec</b>" . date('r') .'<br />'; 
?>